package com.aaa.statistics.service.impl;

import com.aaa.entity.CareOrderItem;
import com.aaa.statistics.dao.MedicinesStsMapper;
import com.aaa.statistics.service.MedicinesStsService;
import com.aaa.statistics.vo.CareOrderItemVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class MedicinesStsServiceImp implements MedicinesStsService {

    @Resource
    private MedicinesStsMapper medicinesStsMapper;

    @Override
    public Result<List<CareOrderItem>> getMedStsList(CareOrderItemVo careOrderItemVo) {
        QueryWrapper<CareOrderItem> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(careOrderItemVo.getDrugName())){
            wrapper.like("item_name",careOrderItemVo.getDrugName());
        }
        if (Objects.nonNull(careOrderItemVo.getDateRange())&&careOrderItemVo.getDateRange().length==2){
            wrapper.between("create_time",careOrderItemVo.getDateRange()[0],careOrderItemVo.getDateRange()[1]);
        }else {
            //当天时间
            String beforeDay = LocalDate.now().minusDays(1).toString();
            String afterDay = LocalDate.now().plusDays(1).toString();
            wrapper.between("create_time",beforeDay,afterDay);
        }
        wrapper.eq("item_type",0);
        List<CareOrderItem> careOrderItems = medicinesStsMapper.selectList(wrapper);
        return new Result<>(200,"查询成功",careOrderItems);
    }

    @Override
    public Result<List<Map<String, Object>>> getMedStatStsList(CareOrderItemVo careOrderItemVo) {
        String drugName = careOrderItemVo.getDrugName();
        if (Objects.nonNull(careOrderItemVo.getDateRange())&&careOrderItemVo.getDateRange().length==2){
            String beforeDate = careOrderItemVo.getDateRange()[0];
            String afterDate = careOrderItemVo.getDateRange()[1];
            List<Map<String, Object>> mapList = medicinesStsMapper.selectMedStatList(drugName,beforeDate,afterDate);
            return new Result<>(200,"查询成功",mapList);
        }else {
            //当天时间
            String beforeDate = LocalDate.now().minusDays(1).toString();
            String afterDate = LocalDate.now().plusDays(1).toString();
            List<Map<String, Object>> mapList = medicinesStsMapper.selectMedStatList(drugName,beforeDate,afterDate);
            return new Result<>(200,"查询成功",mapList);
        }
    }
}
